/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gameheart.client.sprite;

import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Dictionary;
import java.util.Hashtable;
import javax.imageio.ImageIO;

/**
 *
 * @author win7
 */
public class ImageResposity {

    Dictionary<String, BufferedImage> _images;
    GraphicsConfiguration _graphicConfig = null;
    private String _imageDir = "/Images/";

    public ImageResposity() {
        _images = new Hashtable<String, BufferedImage>();
        GraphicsEnvironment graphicEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        _graphicConfig = graphicEnvironment.getDefaultScreenDevice().getDefaultConfiguration();
    }

    public BufferedImage getImage(String fileName) {
        BufferedImage image = _images.get(fileName);
        if (image == null) {
            image = loadImage(fileName);
            _images.put(fileName, image);
        }
        return image;
    }

    private BufferedImage loadImage(String fileName) {
        try {
            BufferedImage im = ImageIO.read(this.getClass().getResource(_imageDir + fileName));

            int transparency = im.getColorModel().getTransparency();
            BufferedImage copy = _graphicConfig.createCompatibleImage(
                    im.getWidth(), im.getHeight(),
                    transparency);
            Graphics2D g2d = copy.createGraphics();

            // copy image
            g2d.drawImage(im, 0, 0, null);
            g2d.dispose();
            return copy;
        } catch (IOException e) {
            System.out.println("Load Image error for " + fileName + ":\n" + e);
            return null;
        }
    }
    public void setIMAGE_DIR(String imageDir)
    {
        _imageDir = imageDir;
    }
}
